package com.magina.nety.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.redisson.api.*;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonJacksonCodec;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Resource
    private RedissonClient reportRedisson;

    @GetMapping("/1")
    public void testMethod1(){
        List<TestDemo> list = Lists.newArrayList();
        for (int i = 0; i < 55; i++) {
            TestDemo testDemo = TestDemo.builder().id(UUID.randomUUID().toString())
                    .date(DateUtil.format(new Date(),"yyyy-MM-dd"))
                    .name("test" + i).build();
            list.add(testDemo);
        }
        RSortedSet rSortedSet = reportRedisson.getSortedSet("test1");
        rSortedSet.addAsync(list);
        RScoredSortedSet rScoredSortedSet = reportRedisson.getScoredSortedSet("test2");

        log.info(rScoredSortedSet.toString());

    }
    @GetMapping("/2")
    public void testMethod2(){
        RScoredSortedSet rScoredSortedSet = reportRedisson.getScoredSortedSet("test4");
        List<TestDemo> list = Lists.newArrayList();
        for (int i = 0; i < 123; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR,i);
            calendar.getTime();
            String dateTime  = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR,1);
            String afterTime  = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            TestDemo testDemo = TestDemo.builder().id(UUID.randomUUID().toString())
                    .date(dateTime + " - " +afterTime)
                    .name("test" + i)
                    .code(i+"code").build();
            list.add(testDemo);
//            rScoredSortedSet.add(i,testDemo);

        }
        Map map = new HashMap();
//        map.put(list,(double)list.size());
//        rScoredSortedSet.addAll(map);
        RList<TestDemo> rList = reportRedisson.getList("test3");
        rList.addAll(list);
        List<TestDemo> rListData = rList.readSort("name",SortOrder.ASC,1,10);
        log.info(rScoredSortedSet.toString());

    }

    @GetMapping("/3")
    public void testMethod3(){
        Codec jsonJacksonCodec = new JsonJacksonCodec(TestDemo.class.getClassLoader());
        RScoredSortedSet<TestDemo> rScoredSortedSet = reportRedisson.getScoredSortedSet("test3",jsonJacksonCodec);
        List<TestDemo> o2 = rScoredSortedSet.stream().collect(Collectors.toList());
        List<TestDemo> o = (List<TestDemo>) rScoredSortedSet.readAll();
//        SortOrder
        Set<TestDemo> o3 = rScoredSortedSet.readSort("name",SortOrder.DESC);
//        Integer count = rScoredSortedSet.sortTo("name",SortOrder.DESC);
        List<String> list =Lists.newArrayList();
        Collection<Object> oo4 = rScoredSortedSet.readSort("name",list,SortOrder.ASC,1,10);
        log.info(rScoredSortedSet.toString());

    }

    @PostMapping("/4")
    public Object testMethod4(@RequestBody Map map){
        Integer pageNum = (Integer) map.get("pageNum");//第几页
        Integer pageSize = (Integer) map.get("pageSize");//每页总数
        Integer startIndex = (pageNum-1)*pageSize;
        Integer endIndex = pageSize*pageNum -1;
        Codec jsonJacksonCodec = new JsonJacksonCodec(TestDemo.class.getClassLoader());
        RList<TestDemo> rList = reportRedisson.getList("testList1",jsonJacksonCodec);
//        List<TestDemo> rListData = rList.readSort("date",SortOrder.ASC,startIndex,pageSize);
        long start = System.currentTimeMillis();
        List<TestDemo> rListData = rList.readSort("date",SortOrder.DESC,startIndex,pageSize);
//        List<TestDemo> rListData1 = rListData.stream().collect(Collectors.toList());
        System.out.println("---4time : " + (System.currentTimeMillis() - start));
        rListData.stream().forEach(p -> System.out.println(p.toString()));
        return rListData;
    }

    @PostMapping("/5")
    public Object testMethod5(@RequestBody Map map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");

        List<TestDemo> list = Lists.newArrayList();
        for (int i = 0; i < 100000; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR,i);
            calendar.getTime();
            String dateTime  = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR,1);
            String afterTime  = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            TestDemo testDemo = TestDemo.builder().id(UUID.randomUUID().toString())
                    .date(dateTime + " - " +afterTime)
                    .name("test" + i)
                    .code(i+"code").build();
            list.add(testDemo);
        }
        Integer startIndex = (pageNum-1)*pageSize;
        Integer endIndex = pageSize*pageNum ;
        long start = System.currentTimeMillis();
        list.sort((a,b) -> b.getDate().compareTo(a.getDate()));
        List<TestDemo> rListData = list.subList(startIndex,endIndex);
        System.out.println("---5time : " + (System.currentTimeMillis() - start));
        rListData.stream().forEach(p -> System.out.println(p.toString()));

        return rListData;
    }


}
